package com.leemz.tgs.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.leemz.tgs.common.JsonResult;
import com.leemz.tgs.domain.Expense;
import com.leemz.tgs.service.ExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/expense")
public class ExpensesController {

    @Autowired
    private ExpenseService expenseService;

    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       String expenseType, BigDecimal priceMin,
                       BigDecimal priceMax) {
        Map<String, Object> params = new HashMap<>();
        params.put("expenseType", expenseType);
        params.put("priceMin", priceMin);
        params.put("priceMax", priceMax);
        PageInfo<Expense> data =  expenseService.listByPage(page, limit, params);
        JsonResult p = new JsonResult().pageSuccess(data.getList(), data.getTotal());
        String objJson = JSON.toJSONString(p);
        System.out.println(objJson);
        return p;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Expense expense) {
        expenseService.insert(expense);
        return new JsonResult().success(null);
    }

    @RequestMapping("/del/{id}")
    @ResponseBody
    public Object delete(@PathVariable("id") Integer id) {
        expenseService.delete(id);
        return new JsonResult().success(null);
    }
}
